
import M from '../../general/utils';

/**
 * 节点
 */
export default class Node{
    constructor(values={}){
        this.values=values;
        this.init();
    }

    /**
     * 节点的类型
     * @returns {{BRANCH: number, NONE: number, LEAF: number}} 0:空,1:枝节点,2:叶节点
     * @constructor
     */
    static get TYPE(){
        return {
            NONE:0,
            BRANCH:1,
            LEAF:2
        };
    }

    /**
     * 从序列中移除子级
     * @param childIndex
     * @param children
     */
    static removeChild(childIndex,children){
        for(let i=childIndex;i<children.length-1;i++){
            if(children[i+1]<0){
                children[i]=-1;
                break;
            }
            children[i]=children[i+1];
        }
    }

    /**
     * 获取此节点的类型
     * @returns {number|null}
     */
    get type(){
        return this._type;
    }

    /**
     * 为节点设置形状
     * @param collider
     */
    set collider(collider){
        if(!collider){
            this._collider=null;
            this._type=Node.TYPE.BRANCH;
            return;
        }
        this._collider=collider;
        this._type=Node.TYPE.LEAF;
    }

    /**
     * 获取节点的形状
     * @returns {null|*}
     */
    get collider(){
        return this._collider;
    }

    /**
     * 获取子节点列表
     * @returns {null|*|Int16Array}
     */
    get children2(){
        return this._children2;
    }

    /**
     * 设置节点的索引
     * @param index
     */
    set index(index){
        this._index=index;
    }

    /**
     * 获取节点的索引
     * @returns {*|null}
     */
    get index(){
        if(this._index===null)console.warn('needs sign a index for this node first!');
        return this._index;
    }

    /**
     * 添加子节点
     * @param nodeIndex
     */
    addChild2(nodeIndex){
        this._childrenBreak++;
        if(this._childrenBreak>this._children2.length-2){
            this.increaseChildrenVolume();
        }

        this._children2[this._childrenBreak-1]=nodeIndex;
        this._children2[this._childrenBreak]=-1;
    }

    /**
     * 移除子节点
     * @param nodeIndex
     */
    removeChild2(nodeIndex){
        for(let i=0;i<this._children2.length;i++){
            if(this._children2[i]<0)break;
            if(this._children2[i]===nodeIndex){
                Node.removeChild(i,this._children2);
                this._childrenBreak--;
                break;
            }
        }
    }

    /**
     * 增加子节点列表的容量
     */
    increaseChildrenVolume(){
        this._children2=M.resizeTypedArray(this._children2,this._children2.length*2);
    }

    /**
     * 销毁
     */
    dispose(){
        this._type=null;
        this._collider=null;
        this._children2=null;
        this._childrenBreak=null;
        this._locationBreak=null;
        this._index=null;
    }

    init(){
        this.initVariable();
    }
    initVariable(){
        this._type=Node.TYPE.NONE;
        this._collider=null;
        this._index=null;

        this._children2=new Int16Array(8);
        this._childrenBreak=0;
        this._children2[this._childrenBreak]=-1;
    }
}